{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running Python code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactively (demo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To run Python code interactively, one can use the standard Python prompt, which can be launched by typing ``python`` in your standard shell:\n", "\n", " $ python\n", " Python 3.4.1 (default, May 21 2014, 21:17:51) \n", " [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin\n", " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", " >>>\n", "\n", "The ``>>>`` indicates that Python is ready to accept commands. If you type ``a = 1`` then press enter, this will assign the value ``1`` to ``a``. If you then type ``a`` you will see the value of ``a`` (this is equivalent to ``print a``):\n", "\n", " >>> a = 1\n", " >>> a\n", " 1\n", "\n", "The Python shell can execute any Python code, even multi-line statements, though it is often more convenient to use Python non-interactively for such cases.\n", "\n", "The default Python shell is limited, so we will use the IPython (or interactive Python) shell here. This is an add-on package that adds many features to the default Python shell, including the ability to edit and navigate the history of previous commands, as well as the ability to tab-complete variable and function names. To start up IPython, type:\n", "\n", " $ ipython\n", " Python 3.4.1 (default, May 21 2014, 21:17:51) \n", " Type \"copyright\", \"credits\" or \"license\" for more information.\n", "\n", " IPython 2.1.0 -- An enhanced Interactive Python.\n", " ? -> Introduction and overview of IPython's features.\n", " %quickref -> Quick reference.\n", " help -> Python's own help system.\n", " object? -> Details about 'object', use 'object??' for extra details.\n", "\n", " In [1]:\n", "\n", "The first time you start up IPython, it will display a message which you can skip over by pressing ``ENTER``. The ``>>>`` symbols are now replaced by ``In [x]``, and output, when present, is prepended with ``Out [x]``. If we now type the same commands as before, we get:\n", "\n", " In [1]: a = 1\n", "\n", " In [2]: a\n", " Out[2]: 1\n", "\n", "If you now type the up arrow twice, you will get back to ``a = 1``.\n", "\n", "To exit the Python shell at any time and return to the command prompt of the terminal, type ``exit()``." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }